<template>
  <el-container>
    <el-header>
      <div>电商后台管理系统</div>
      <el-button type="text" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu :default-active="defaultActive" class="el-menu-vertical-demo" background-color="#545c64"
          text-color="#fff" active-text-color="#ffd04b" :unique-opened="true" router>
          <!--一级菜单-->
          <el-submenu :index="menu.index+''" v-for="menu in menuList" :key="menu.index">
            <template slot="title">
              <i :class="menu.icon"></i>
              <span>{{menu.name}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="item.path+''" v-for="item in menu.children" :key="item.index"
              @click="saveDefaultActive(item.path)">
              <i :class="item.path"></i>
              <span slot="title">{{item.name}}</span>
            </el-menu-item>
          </el-submenu>

          <!-- <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-s-goods"></i>
              <span>商品管理</span>
            </template> -->
          <!--二级菜单-->
          <!-- <el-menu-item index="getGoodsList" @click="saveDefaultActive('getGoodsList')">
              <i class="el-icon-menu"></i>
              <span slot="title">商品列表</span>
            </el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  // @ is an alias to /src
  import HelloWorld from '@/components/HelloWorld.vue'

  export default {
    name: 'Home',
    components: {
      HelloWorld
    },
    created() {
      this.getMenuList()
      this.defaultActive = sessionStorage.getItem("defaultActive")
    },
    methods: {
      async getMenuList() {
        const {
          data: res
        } = await this.$axios.post('/getMenuList', {})
        console.log(res)
        this.menuList = res.result
      },
      logout() {
        sessionStorage.removeItem("token")
        this.$router.push('/login')
      },
      saveDefaultActive(path) {
        // console.log(path)
        sessionStorage.setItem("defaultActive", path)
        this.defaultActive = path
      }
    },
    data() {
      return {
        defaultActive: '',
        menuList: []
      }
    }
  }
</script>

<style scoped="scoped">
  .el-container {
    height: 100%;
  }

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-header div {
    font-size: 20px;
  }

  .el-aside {
    background-color: rgb(84, 92, 100);
  }

  .el-menu {
    border-right: none;
  }
</style>
